# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=pyzopfli
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.2.2
pkgrel=2
pkgdesc="Python bindings for the Zopfli compressor (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://github.com/fonttools/py-zopfli/'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools-scm"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('staticlibs' 'strip' '!debug')
source=("https://files.pythonhosted.org/packages/source/z/zopfli/zopfli-${pkgver}.zip"
        win32-fixes.patch)
sha256sums=('2d49db7540d9991976af464ebc1b9ed12988c04d90691bcb51dc4a373a9e2afc'
            '3adb302978fb0c47adf0eeb641b6674add71fd6af9a22497fbeffb991e78dddd')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  pushd "${srcdir}/zopfli-${pkgver}/zopfli"
  apply_patch_with_msg win32-fixes.patch
  popd

  rm -rf python-build-${MSYSTEM} | true
  cp -r "zopfli-${pkgver}" "python-build-${MSYSTEM}"

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py check
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"
}
